<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%
    String path = request.getContextPath();
    String basePath = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + path + "/";
%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@ include file="/head.jsp" %>

<body>

<div>
    <div class="layui-form layui-form-pane" style="margin: 5px">

        <button class="layui-btn" id="add" lay-even="add"
               style="visibility: hidden">添加
        </button>
<%--        <button class="layui-btn" id="add" lay-even="add"--%>
<%--                <c:if test="${model.glCreate != 1}">style="visibility: hidden"</c:if>>添加--%>
<%--        </button>--%>
        <%--<button class="layui-btn" data-type="update"--%>
                <%--<c:if test="${model.glUpdate != 1}">style="visibility: hidden"</c:if>>Update--%>
        <%--</button>--%>
        <%--<button class="layui-btn layui-btn-danger" data-type="dels"--%>
                <%--<c:if test="${model.glDelete != 1}">style="visibility: hidden"</c:if>>Delete--%>
        <%--</button>--%>

        <div class="layui-inline" style="float: right">
            <div class="layui-input-inline">
                <div class="layui-input-block">
                    <input type="text" name="searchName" id="searchName"
                           placeholder="请输入昵称或手机号" autocomplete="off" class="layui-input">
                </div>
            </div>
            <button class="layui-btn" data-type="reload">搜索</button>
        </div>
    </div>

    <div class="layui-form layui-form-pane" style="margin: 5px">
        <table id="demo" lay-filter="test"></table>
    </div>
</div>

<%--序号--%>
<script type="text/html" id="increase">
    {{d.LAY_TABLE_INDEX+1}}
</script>

<script type="text/html" id="createTime">
    {{# return layui.util.toDateString(d.createTime, 'yyyy-MM-dd HH:mm:ss') }}
</script>
<script type="text/html" id="userNickName">
    {{d.userNickName}}
    {{# if(d.isTeacher == 2){}}
    <span style="color:#ff0000">【讲师认证】</span>
    {{# }}}
</script>

<%--图片转换--%>
<script type="text/html" id="userImg">
    <ul class="showImg">
        {{# for (var i=0,len=d.userImg.split(',');i<len.length;i++){}}

            {{# if(len[i] != '' && len[i].indexOf("http") == -1){}}
                <li><img src="${url}{{ len[i]}}"  alt="Picture{{i}}"></li>
            {{# } else { }}
                <li><img src="{{ len[i]}}"  alt="Picture{{i}}"></li>
            {{# }}}

        {{# }}}
    </ul>
</script>



<script type="text/html" id="barDemo">
<%--    <a class="layui-btn layui-btn-xs" lay-event="checkSellOrders">售出订单</a>--%>
    {{# if(d.userStatus == 1){}}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="joinBlack">拉黑</a>
    {{# }}}
    {{# if(d.isTeacher != 2){}}
    <a class="layui-btn layui-btn-xs" lay-event="makeTeacher">指定讲师</a>
    {{# }}}
    {{# if(d.isTeacher == 2){}}
<%--    <a class="layui-btn layui-btn-xs" lay-event="dismissTeacher">辞退讲师</a>--%>
    {{# }}}
    {{# if(d.userStatus == 2){}}
    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="removeJoinBlack">解除拉黑</a>
    {{# }}}
</script>


<script>
    layui.use(['util', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element', 'form'], function () {
        var table = layui.table,
            form = layui.form,
            $ = layui.jquery,//加载jquery对象
            layer = layui.layer;

        //让层自适应iframe
        $('#add').on('click', function () {
            var index = layer.open({
                title: '信息',
                type: 2,
                content: '/manage/user/goAdd',
                area: ['800px', '450px'],
                maxmin: true,
                end: function () {
                    location.reload();
                }
            });
            parent.layer.iframeAuto(index);
        });


        //第一个实例
        table.render({
            elem: '#demo'
            , url: '/manage/webUser/findUserWebData' //数据接口
            , page: true //开启分页
            , limit: 10 //初始页条数
            , cols: [[ //表头
                 {field: 'id', title: 'ID', width: 60, templet: '#increase'}
                , {field: 'userPhone', width: 200, title: '手机号'}
                , {field: 'userNickName',width: 250, title: '昵称',templet: '#userNickName'}
                , {field: 'userImg', title: '头像', templet: '#userImg',width: 150}
                , {field: 'balance', title: '余额',sort:true ,width: 150}
                , {field: 'createTime',width: 250, title: '注册时间', templet: '#createTime',sort:true}
                , {fixed: 'right', title: '操作栏', align: 'left', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器
            ]]
            , done: function () {
                $(".showImg").viewer();
            }
            , id: 'testReload' //重载标识
        });

        var active = {
            // 方法级渲染的重载 不需要 #
            reload: function () {
                //获取条件
                var searchName = $('#searchName').val();

                //执行重载 testReload 对应第一个实例中 id
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        searchName: searchName
                    }
                });
            }
            // 获取checkStatus行数据
            , dels: function () { //获取选中数据 demo 为table的id
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                var userIds = [];
                data.forEach(function (item, index) {
                    userIds.push(item.userId);
                });
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/webUser/dels',
                        type: 'post',
                        dataType: 'json',
                        data: {userIds: userIds.join(",")},//join 将数组组装成 1,2,3 的形式
                        success: function (data) {
                            if (data.code == 200) {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }
            , update: function () { //获取选中数据 demo 为table的id   data
                var checkStatus = table.checkStatus('testReload')
                    , data = checkStatus.data;
                if (data.length != 1) {
                    layer.msg("Select an action");
                    return false;
                }
                //data 获取的是数组,所以当修改的时候就要获取第一个数据
                var userId = data[0].userId;
                var index = layer.open({
                    title: '信息',
                    title: '',
                    type: 2,
                    content: '/manage/webUser/goEdit?userId=' + userId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            }
        };

        $('.layui-form .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //监听工具条
        table.on('tool(test)', function (obj) { //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象

            if (layEvent === 'checkSellOrders') { //查看
                window.location.href = "/manage/webUser/findOrdersByPublishUserId?userId="+data.userId;
            }else if (layEvent === 'removeJoinBlack') { //解除拉黑
                layer.confirm('真的解除拉黑吗?', function (index) {
                    $.ajax({
                        url: '/manage/webUser/removeJoinBlack',
                        type: 'post',
                        dataType: 'json',
                        data: {userId: data.userId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            }else if (layEvent === 'makeTeacher') { //指定讲师
                  layer.open({
                        type: 1,
                        title: '请输入讲师简介',
                        area: ['550px', '350px'],
                        btn: ['确定'],
                        content: '<div><textarea type="text"  autocomplete="off" class="layui-textarea" lay-verify="required" style="height:200px"></textarea></div>',//这里content是一个普通的String
                        yes: function (index, layero) {
                            //按钮【按钮一】的回调
                            var backreason = layero.find('textarea')[0].value;// 获取输入的讲师简介
                            layer.closeAll();
                            $.ajax({
                                url: '/manage/webUser/makeTeacher',
                                type: 'post',
                                dataType: 'json',
                                data: {userId: data.userId,userDesc:backreason},
                                success: function (data) {
                                    if (data.code == "200") {
                                        layer.msg(data.msg, {icon: 1, time: 1000}, function () {
                                            window.location.reload();
                                        });
                                    } else {
                                        layer.msg(data.msg, {icon: 1, time: 1000});
                                    }
                                }
                            })
                        }
                    });
            }else if (layEvent === 'dismissTeacher') { //辞退讲师
                layer.confirm('确定辞退讲师吗?', function (index) {
                    $.ajax({
                        url: '/manage/webUser/dismissTeacher',
                        type: 'post',
                        dataType: 'json',
                        data: {userId: data.userId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500, area:['400px', '500px']}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            } else if (layEvent === 'joinBlack') { //拉黑
                layer.confirm('真的拉黑吗?', function (index) {
                    $.ajax({
                        url: '/manage/webUser/joinBlack',
                        type: 'post',
                        dataType: 'json',
                        data: {userId: data.userId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            } else if (layEvent === 'del') { //删除
                layer.confirm('真的删除吗?', function (index) {
                    $.ajax({
                        url: '/manage/webUser/del',
                        type: 'post',
                        dataType: 'json',
                        data: {id: data.userId},
                        success: function (data) {
                            if (data.code == "200") {
                                layer.msg(data.msg, {icon: 1, time: 500}, function () {
                                    window.location.reload();
                                });
                            } else {
                                layer.msg(data.msg, {icon: 1, time: 1000});
                            }
                        }
                    })
                });
            } else if (layEvent === 'edit') { //编辑
                var index = layer.open({
                    title: '信息',
                    type: 2,
                    content: '/manage/webUser/goEdit?userId=' + data.userId,
                    area: ['800px', '450px'],
                    maxmin: true,
                    end: function () {
                        location.reload();
                    }
                });
                parent.layer.iframeAuto(index);
            } else if (layEvent === 'openImg') { //监听单元格
                //页面层-展示图片
                layer.open({
                    type: 1,
                    title: false,
                    closeBtn: 1,
                    area: ['450px', '450px'],
                    skin: 'layer-ext-myskin', //没有背景色
                    shadeClose: true,
                    content: '<img src="${url}' + data.userImg + '" height="450px" width="450px" alt="Load failed"/>'
                });
            }
        });

        var getIds = function () {
            var ids = [];
            var checkStatus = table.checkStatus('testReload')
                , data = checkStatus.data;
            $.each(data, function (index, item) {
                ids.push(item['userId'])
            });
            return ids;
        }

    });
</script>

<%@ include file="/foot.jsp" %>
